package com.juyu.miliao.common.util;

import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class DateUtil {
	private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
	private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

	public static LocalDateTime parseDateTime(String str){
		return LocalDateTime.parse(str, dateTimeFormatter);
	}
	
	public static LocalDate parseDate(String str){
		return LocalDate.parse(str, dateFormatter);
	}
	
	public static String format(LocalDateTime dateTime){
		return dateTimeFormatter.format(dateTime);
	}
	
	public static String format(LocalDate date){
		return dateFormatter.format(date);
	}

	/**
	 * 当前日期时间距离目标日期时间的剩余时间
	 * @param targetTime
	 * @param timeUnit
	 * @return
	 */
	public static long getTheRemainingTimesOfTheDay(LocalDateTime targetTime,TimeUnit timeUnit){

		if(timeUnit.equals(TimeUnit.NANOSECONDS)){
			long nanoSeconds = ChronoUnit.NANOS.between(LocalDateTime.now(),targetTime);
			return nanoSeconds;
		}else if(timeUnit.equals(TimeUnit.MICROSECONDS)){
			long microSeconds = ChronoUnit.MICROS.between(LocalDateTime.now(),targetTime);
			return microSeconds;
		}else if(timeUnit.equals(TimeUnit.MILLISECONDS)){
			long millSeconds = ChronoUnit.MILLIS.between(LocalDateTime.now(),targetTime);
			return millSeconds;
		}else if(timeUnit.equals(TimeUnit.SECONDS)){
			long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), targetTime);
			return seconds;
		}else if(timeUnit.equals(TimeUnit.MINUTES)){
			long minutes = ChronoUnit.MINUTES.between(LocalDateTime.now(), targetTime);
			return minutes;
		}else if(timeUnit.equals(TimeUnit.HOURS)){
			long hour = ChronoUnit.HOURS.between(LocalDateTime.now(), targetTime);
			return hour;
		}else if(timeUnit.equals(TimeUnit.DAYS)){
			long day = ChronoUnit.DAYS.between(LocalDateTime.now(), targetTime);
			return day;
		}else {
			throw new RuntimeException("传入TimeUnit不存在");
		}

	}

	/**
	 * 目标日期时间距离当前日期时间的剩余时间
	 * @param targetTime
	 * @param timeUnit
	 * @return
	 */
	public static long getBetweenTime(LocalDateTime targetTime,TimeUnit timeUnit){

		if(timeUnit.equals(TimeUnit.NANOSECONDS)){
			long nanoSeconds = ChronoUnit.NANOS.between(targetTime,LocalDateTime.now());
			return nanoSeconds;
		}else if(timeUnit.equals(TimeUnit.MICROSECONDS)){
			long microSeconds = ChronoUnit.MICROS.between(targetTime,LocalDateTime.now());
			return microSeconds;
		}else if(timeUnit.equals(TimeUnit.MILLISECONDS)){
			long millSeconds = ChronoUnit.MILLIS.between(targetTime,LocalDateTime.now());
			return millSeconds;
		}else if(timeUnit.equals(TimeUnit.SECONDS)){
			long seconds = ChronoUnit.SECONDS.between(targetTime,LocalDateTime.now());
			return seconds;
		}else if(timeUnit.equals(TimeUnit.MINUTES)){
			long minutes = ChronoUnit.MINUTES.between(targetTime,LocalDateTime.now());
			return minutes;
		}else if(timeUnit.equals(TimeUnit.HOURS)){
			long hour = ChronoUnit.HOURS.between(targetTime,LocalDateTime.now());
			return hour;
		}else if(timeUnit.equals(TimeUnit.DAYS)){
			long day = ChronoUnit.DAYS.between(targetTime,LocalDateTime.now());
			return day;
		}else {
			throw new RuntimeException("传入TimeUnit不存在");
		}
	}


	public static String getShowTime(LocalDateTime time){

		long oneHour = 60;

		long oneDay = oneHour*24;

		long twoDay = oneDay*2;



		long minutes = DateUtil.getBetweenTime(time, TimeUnit.MINUTES);




		if(minutes==0){
			return "刚刚";
		}

		if(minutes>0&&minutes<=oneHour){
			return minutes+"分钟前";
		}

		if(minutes > oneHour && minutes<oneDay){

			long hour = DateUtil.getBetweenTime(time, TimeUnit.HOURS);

			return hour+"小时前";
		}

		if(minutes>=oneDay&&minutes<oneDay*3){
			long day = DateUtil.getBetweenTime(time,TimeUnit.DAYS);
			return day + "天前";
		}else{
			return "3天前";
		}


	}

	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0) {
			return "00:00";
		} else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = unitFormat(minute) + ":" + unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99) {
					return "99:59:59";
				}
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
			}
		}
		return timeStr;
	}

	public static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10) {
			retStr = "0" + Integer.toString(i);
		}
		else {
			retStr = "" + i;
		}
		return retStr;
	}

	/**
	 * 获取当前日期是今年的第几周
	 * @param minimalDaysInFirstWeek 一周开始的星期
	 * @param localDate
	 * @return
	 */
	public static int getWeeksByYear(int minimalDaysInFirstWeek,LocalDate localDate){
		WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,minimalDaysInFirstWeek);
		return localDate.get(weekFields.weekOfYear());
	}


	/**
	 * 判断 当前时间是否在开始于结束时间
	 * @param startDate
	 * @param endDate
	 * @param localDate
	 * @return
	 */
	public static boolean checkBetweenTime(LocalDate startDate,LocalDate endDate,LocalDate localDate){
		boolean bl = false;
		if(localDate.isAfter(startDate)||localDate.equals(startDate)) {
			if (localDate.isBefore(endDate) || localDate.equals(endDate)) {
				bl = true;
			}
		}

		return bl;
	}

	/**
	 * 获取当前时间是今年的第几周的开始时间和结束时间
	 * @param localDate
	 * @return
	 */
	public static JSONObject getWeekByYearTime(LocalDate localDate){
		int year = localDate.getYear();//获取年份
		int week = getWeeksByYear(1,localDate);//获取当前日期是今年的第几周
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, 0, 1);
		int dayOfWeek = 7- calendar.get(Calendar.DAY_OF_WEEK) + 1;//算出第一周还剩几天 +1是因为1号是1天
		week = week -2;//周数减去第一周再减去要得到的周
		calendar.add(Calendar.DAY_OF_YEAR, week*7+dayOfWeek+1);//+1 起始日期从周一开始
		String startTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		calendar.add(Calendar.DAY_OF_YEAR, 6);//开始时间+6天得到结束时间
		String endTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("startTime",startTime);
		jsonObject.put("endTime",endTime);
		return jsonObject;
	}

	/**
	 * 获取当前时间的前i天的日期
	 * @return
	 * @throws ParseException
	 */
	private static String getStateTime(int i) throws ParseException {
		//yyyy-MM-dd
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar c = Calendar.getInstance();

		c.add(Calendar.DATE, - i);

		Date time = c.getTime();

		String preDay = sdf.format(time);

		return preDay;
	}

	/**
	 * 获取当天的前7天所有日期
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getStateTime() throws ParseException {
		List<String> list = new ArrayList<>();
		for(int i = 0; i<7;i++){
			 String stateTime = getStateTime(i + 1);
			list.add(stateTime);
		}

		return list;
	}

	public static String getSingOutDateTime(long timestamp){
		long nowTimestamp = Instant.now().toEpochMilli();// 精确到毫秒
		long difference = nowTimestamp - timestamp;
		int oneMinute = 60000;
		int oneHourse = oneMinute * 60;
		int oneDay = oneHourse * 24;
		if(difference < oneHourse){
			if(difference <= 0 || difference<(oneMinute*5)){
				return "1分钟前";
			}
			if(difference>=(oneMinute*5) && difference<(oneMinute*30)){
				return "5分钟前";
			}
			if(difference>=(oneMinute*30) && difference<(oneMinute*60)){
				return "30分钟前";
			}
		}else if(difference < oneDay){
			for(int i =2;i < 25;i++){
				if(oneHourse*(i-1) <= difference && difference < oneHourse*i){
					return (i-1)+"小时前";
				}
			}
		}else{
			for(int i=2 ; i<8 ; i++){
				if(difference>=oneDay*(i-1) && difference< oneDay*i){
					return (i-1)+"天前";
				}
			}
		}
		return "7天前";
	}

}
